package net.lab0.wallstreet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import net.lab0.tools.StaticRandom;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class DataSourceTest
{
    private static final String        testFile         = "./tst/input_test.txt";
    private static final List<Integer> expectedPayments = new ArrayList<Integer>();
    private static final int           expectedTransfer = 116;
    private static final DecimalFormat format           = new DecimalFormat("#.00");
    
    private DataSource                 dataSource;
    
    /**
     * Creates the data to read
     * 
     * @throws IOException
     */
    @BeforeClass
    public static void beforeClass()
    throws IOException
    {
        File input = new File(testFile);
        if (!input.getParentFile().exists())
        {
            input.getParentFile().mkdirs();
        }
        PrintWriter printWriter = null;
        
        try
        {
            printWriter = new PrintWriter(testFile);
            printWriter.println(format.format(expectedTransfer / 100.0));
            for (int i = 0; i < StaticRandom.random.nextInt(100) + 50; ++i)
            {
                int value = StaticRandom.random.nextInt(20 * 1000);
                printWriter.println(format.format(value / 100.0));
                expectedPayments.add(value);
            }
        }
        finally
        {
            if (printWriter != null)
            {
                printWriter.close();
            }
        }
    }
    
    @Before
    public void readFile()
    throws IOException
    {
        dataSource = new DataSource(testFile);
        dataSource.readData();
    }
    
    @Test
    public void testValues()
    {
        Assert.assertEquals(expectedTransfer, dataSource.getBankTransfer());
        Assert.assertEquals(expectedPayments, dataSource.getDuePayments());
    }
}
